﻿using Microsoft.AspNetCore.Mvc;
using OrderStateMachineWebApi.Models;
using OrderStateMachineWebApi.Services;

namespace OrderStateMachineWebApi.Controllers
{

    [ApiController]
    [Route("api/[controller]")]
    public class OrderController : ControllerBase
    {
        private readonly IOrderStateMachineService _orderService;
        private readonly IOrderRepository _orderRepository;

        public OrderController(IOrderStateMachineService orderService, IOrderRepository orderRepository)
        {
            _orderService = orderService;
            _orderRepository = orderRepository;
        }

        /// <summary>
        /// 创建新订单
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<Order>> CreateOrder(decimal amount)
        {
            if (amount <= 0)
            {
                return BadRequest("订单金额必须大于0");
            }

            var order = await _orderService.CreateOrderAsync(amount);
            return CreatedAtAction(nameof(GetOrder), new { id = order.Id }, order);
        }

        /// <summary>
        /// 获取所有订单
        /// </summary>
        [HttpGet]
        public async Task<ActionResult> GetAllOrders()
        {
            var orders = await _orderRepository.GetAllOrdersAsync();
            return Ok(orders);
        }

        /// <summary>
        /// 获取订单详情
        /// </summary>
        [HttpGet("{id}")]
        public async Task<ActionResult<Order>> GetOrder(Guid id)
        {
            var order = await _orderService.GetOrderByIdAsync(id);
            if (order == null)
            {
                return NotFound();
            }
            return Ok(order);
        }

        /// <summary>
        /// 支付订单
        /// </summary>
        [HttpPost("{id}/pay")]
        public async Task<ActionResult<Order>> PayOrder(Guid id)
        {
            try
            {
                var order = await _orderService.PayOrderAsync(id);
                return Ok(order);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 处理订单
        /// </summary>
        [HttpPost("{id}/process")]
        public async Task<ActionResult<Order>> ProcessOrder(Guid id)
        {
            try
            {
                var order = await _orderService.ProcessOrderAsync(id);
                return Ok(order);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 发货
        /// </summary>
        [HttpPost("{id}/ship")]
        public async Task<ActionResult<Order>> ShipOrder(Guid id)
        {
            try
            {
                var order = await _orderService.ShipOrderAsync(id);
                return Ok(order);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 完成订单
        /// </summary>
        [HttpPost("{id}/complete")]
        public async Task<ActionResult<Order>> CompleteOrder(Guid id)
        {
            try
            {
                var order = await _orderService.CompleteOrderAsync(id);
                return Ok(order);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 取消订单
        /// </summary>
        [HttpPost("{id}/cancel")]
        public async Task<ActionResult<Order>> CancelOrder(Guid id)
        {
            try
            {
                var order = await _orderService.CancelOrderAsync(id);
                return Ok(order);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 开始退款
        /// </summary>
        [HttpPost("{id}/refund/start")]
        public async Task<ActionResult<Order>> StartRefund(Guid id)
        {
            try
            {
                var order = await _orderService.StartRefundOrderAsync(id);
                return Ok(order);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 完成退款
        /// </summary>
        [HttpPost("{id}/refund/complete")]
        public async Task<ActionResult<Order>> CompleteRefund(Guid id)
        {
            try
            {
                var order = await _orderService.CompleteRefundOrderAsync(id);
                return Ok(order);
            }
            catch (KeyNotFoundException)
            {
                return NotFound();
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }

}
